﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Client.UserCtl
{
    public partial class UserCtl
    {
        private Utils.MSSql FDBControl = new Utils.MSSql();
        //private ModIO FIOControl = new ModIO();
        /// <summary>
        /// 取指定年度企业类型
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="year">年度</param>
        /// <returns></returns>
        public int GetCompanyTypeByYear(int uid, int year)
        {
            try
            {
                if (uid <= 0)
                    throw new Exception("参数错误!");
                if (year <= 0)
                    throw new Exception("参数错误!");
                string sqlstr = "select CTid from CA_YType where CYear=" + year.ToString() + " and YUid=" + uid.ToString();
                DataTable dt = FDBControl.Query(sqlstr);
                if (dt.Rows.Count < 1)
                    return 2;
                return (int)dt.Rows[0]["CTid"];
            }
            catch (Exception ex) { throw new Exception(ex.Message); }
        }
        /// <summary>
        /// 修改或增加指定年度的企业类型
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="year">年度</param>
        /// <param name="tid">企业类型ID</param>
        /// <returns></returns>
        public string WriteCompanyType(int uid, int year, int tid)
        {
            try
            {
                if (uid <= 0)
                    throw new Exception("参数错误");
                if (year <= 0)
                    throw new Exception("参数错误");
                if (tid <= 0)
                    throw new Exception("参数错误");
                string sqlstr = "select count(*) as a from CA_YType where YUid=" + uid.ToString() + " and CYear=" + year.ToString();
                DataTable dt = FDBControl.Query(sqlstr);
                if (dt.Rows.Count < 1)
                    throw new Exception("未知错误");
                int sk = (int)dt.Rows[0]["a"];
                if (sk > 0)
                {
                    //修改
                    sqlstr = "update CA_YType set CTid=" + tid.ToString() + " where YUid=" + uid.ToString() + " and CYear=" + year.ToString();
                }
                else
                {
                    //新增
                    sqlstr = "insert into CA_YType(YUid,CTid,CYear) values(" + uid.ToString() + "," + tid.ToString() + "," + year.ToString() + ")";
                }
                FDBControl.Exec(sqlstr);
                return "";
            }
            catch (Exception ex) { throw new Exception(ex.Message); }
        }
        /// <summary>
        /// 获取所有年度企业类型
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <returns>返回2维数组，0=年度 1=类型ID 2=类型名称</returns>
        public string[][] GetCompanyTypeList(int uid)
        {
            try
            {
                if (uid <= 0)
                    throw new Exception("参数错误");
                string sqlstr = "select t.CYear ,t.CTid ,t1.FName  from CA_YType t left join CA_CType t1 on t.CTid=t1.id where t.YUid=" + uid.ToString() + " order by t.CYear";
                DataTable dt = FDBControl.Query(sqlstr);
                string[][] s = new string[dt.Rows.Count][];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    s[i] = new string[3];
                    s[i][0] = dt.Rows[i]["CYear"].ToString();
                    s[i][1] = dt.Rows[i]["CTid"].ToString();
                    s[i][2] = dt.Rows[i]["FName"].ToString();
                }
                return s;
            }
            catch (Exception ex) { throw new Exception(ex.Message); }
        }

    }
}

